package com.ruoyi.web.controller.system;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.generator.domain.GenTable;
import com.ruoyi.system.domain.DocGenTable;
import com.ruoyi.system.service.IDocGenTableService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.DocGenTableColumn;
import com.ruoyi.system.service.IDocGenTableColumnService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 代码生成业务字段Controller
 * 
 * @author ruoyi
 * @date 2021-10-11
 */
@RestController
@RequestMapping("/system/column")
public class DocGenTableColumnController extends BaseController
{
    @Autowired
    private IDocGenTableColumnService docGenTableColumnService;

    @Autowired
    private IDocGenTableService docGenTableService;
    /**
     * 查询代码生成业务字段列表
     */
    @PreAuthorize("@ss.hasPermi('system:column:list')")
    @GetMapping("/list")
    public TableDataInfo list(DocGenTableColumn docGenTableColumn)
    {
        startPage();
        List<DocGenTableColumn> list = docGenTableColumnService.selectDocGenTableColumnList(docGenTableColumn);
        return getDataTable(list);
    }

    /**
     * 导出代码生成业务字段列表
     */
    @PreAuthorize("@ss.hasPermi('system:column:export')")
    @Log(title = "代码生成业务字段", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(DocGenTableColumn docGenTableColumn)
    {
        List<DocGenTableColumn> list = docGenTableColumnService.selectDocGenTableColumnList(docGenTableColumn);
        ExcelUtil<DocGenTableColumn> util = new ExcelUtil<DocGenTableColumn>(DocGenTableColumn.class);
        return util.exportExcel(list, "代码生成业务字段数据");
    }

    /**
     * 获取代码生成业务字段详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:column:query')")
    @GetMapping(value = "/{columnId}")
    public AjaxResult getInfo(@PathVariable("columnId") Long columnId)
    {
        return AjaxResult.success(docGenTableColumnService.selectDocGenTableColumnByColumnId(columnId));
    }

    /**
     *  根据表ID查询表信息以及字段信息
     */
    @PreAuthorize("@ss.hasPermi('system:table:query')")
    @GetMapping(value = "/getInfoByTableId/{tableId}")
    public AjaxResult getInfoByTableId(@PathVariable("tableId") Long tableId)
    {
        DocGenTable table = docGenTableService.selectDocGenTableById(tableId);
        List<DocGenTable> tables = docGenTableService.selectDocGenTableAll();

        List<DocGenTableColumn> list = docGenTableColumnService.selectDocGenTableColumnListByTableId(tableId);
        Map<String, Object> map = new HashMap<String, Object>();
        // 存储表基本信息
        map.put("info", table);
        // 存储表所列字段信息
        map.put("rows", list);
        // 存储所有表信息
        map.put("tables", tables);
        return AjaxResult.success(map);
    }

    /**
     * 修改保存代码生成业务
     */
    @PreAuthorize("@ss.hasPermi('system:column:edit')")
    @Log(title = "代码生成业务字段", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult editSave(@Validated @RequestBody DocGenTable docgenTable)
    {
        docGenTableService.validateEdit(docgenTable);
        docGenTableService.updateGenTable(docgenTable);
        return AjaxResult.success();
    }

    /**
     * 新增代码生成业务字段
     */
    @PreAuthorize("@ss.hasPermi('system:column:add')")
    @Log(title = "代码生成业务字段", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DocGenTableColumn docGenTableColumn)
    {
        return toAjax(docGenTableColumnService.insertDocGenTableColumn(docGenTableColumn));
    }

    /**
     * 修改代码生成业务字段
     */
    @PreAuthorize("@ss.hasPermi('system:column:edit')")
    @Log(title = "代码生成业务字段", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody DocGenTableColumn docGenTableColumn)
    {
        return toAjax(docGenTableColumnService.updateDocGenTableColumn(docGenTableColumn));
    }



    /**
     * 删除代码生成业务字段
     */
    @PreAuthorize("@ss.hasPermi('system:column:remove')")
    @Log(title = "代码生成业务字段", businessType = BusinessType.DELETE)
	@DeleteMapping("/{columnIds}")
    public AjaxResult remove(@PathVariable Long[] columnIds)
    {
        return toAjax(docGenTableColumnService.deleteDocGenTableColumnByColumnIds(columnIds));
    }
}
